package com.xcj.juhe.tool;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import java.util.Properties;

/**
 * @author xcj
 * @date 2021/7/14 10:26
 */
public class SshConnectionTool {
    //ssh中的knownhost文件名
    private final static String S_PATH_FILE_KNOWN_HOSTS = "~/.ssh/known_hosts";
    //ssh连接的用户名
    private final static String SSH_USER = "dctest";
    //ssh连接的密码
    private final static String SSH_PASSWORD = "OTL#Kxv6!4KzWhpaB33";
    //ssh远程连接的ip地址
    private final static String SSH_REMOTE_SERVER = "47.112.140.239";
    //ssh连接的端口号
    private final static int SSH_REMOTE_PORT = 36000;
    //本地mysql发起连接的IP地址
    private final static String MYSQL_REMOTE_SERVER = "127.0.0.1";
    //本地数据库连接时用的端口号
    private final static int LOCAl_PORT = 3307;
    //远程数据库端口用的端口号
    private final static int REMOTE_PORT = 3306;

    private Session sesion; //represents each ssh session

    public void closeSSH ()
    {
        sesion.disconnect();
    }

    public SshConnectionTool () throws Throwable
    {

        JSch jsch = null;

        jsch = new JSch();
//      设置文件路径，如：~/.ssh/known_hosts（known_hosts中存储是已认证的远程主机host key）
        jsch.setKnownHosts(S_PATH_FILE_KNOWN_HOSTS);
//        设置私钥
//        jsch.addIdentity(S_PATH_FILE_PRIVATE_KEY);

        sesion = jsch.getSession(SSH_USER, SSH_REMOTE_SERVER, SSH_REMOTE_PORT);

        sesion.setPassword(SSH_PASSWORD);

        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        sesion.setConfig(config);

        sesion.connect(); //ssh connection established!

        //by security policy, you must connect through a fowarded port
        sesion.setPortForwardingL(LOCAl_PORT, MYSQL_REMOTE_SERVER, REMOTE_PORT);

    }


}
